
package com.study.algorithm.utils;

import java.util.ArrayList;
import java.util.List;

public class ListUtil {

    public static <T> List<T> ListOf(T... args) {
        int size = args.length;
        List<T> list = new ArrayList<>(size);
        if (size == 0) {
            return list;
        }
        for (T arg : args) {
            list.add(arg);
        }
        return list;
    }

    public static <T> List<T> DeepClone(List<T> originList) {
        List<T> destList = new ArrayList<>(originList.size());
        for (T element : originList) {
            destList.add(element);
        }
        return destList;
    }

    /**
     * 交换坐标不同的两个元素值
     *
     * @param list
     * @param index1
     * @param index2
     * @param <T>
     */
    public static <T> void ExchangeValue(List<T> list, int index1, int index2) {
        //使用两个元素值方便理解
        if (index1 == index2) {
            return;
        }
        T val1 = list.get(index1);
        T val2 = list.get(index2);
        list.set(index1, val2);
        list.set(index2, val1);
    }

}
